
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<errno.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include "data_global.h"
#include "uart_api.h"


void print_storage_allenv_info(struct storage_info *s_info);

extern struct env_info_clien_addr all_info_RT;



//:接收M0数据线程.
void *pthread_transfer(void *arg)
{
	printf("pthread_transfer\n");
	
	int fd = open_port("/dev/ttyUSB0");
	if(fd < 0){
		printf("open failed\n");
		return -1;
	}
	set_com_config(fd, 115200, 8, 'N', 1);
	
	char buf[sizeof(struct storage_info)+2]={0};
	printf("sizeof(buf) = %d.\n",sizeof(buf));

	while(1){
		memset(buf,0,sizeof(buf));
		recvDats(fd,&buf[0],1);
		if(buf[0] != 0x55){
			continue;
		}
		recvDats(fd,&buf[1],1);
		if(buf[1] != 0xaa){
			continue;
		}
		recvDats(fd,&buf[2],sizeof(buf)-2);
		memcpy(&(all_info_RT.storage_no[1]),buf+2,sizeof(buf)-2);
		print_storage_allenv_info(&(all_info_RT.storage_no[1]));
		sleep(1);
	}

	close(fd);

}



void print_storage_allenv_info(struct storage_info *s_info)
{
	int i = 0;
		printf("******************************************\n");
		printf("storage_status = %d \n",s_info->storage_status);
		printf("led_status = %d \n",s_info->led_status);	
		printf("buzzer_status = %d \n",s_info->buzzer_status);
		printf("fan_status = %d \n",s_info->fan_status);
		printf("seg_status = %d \n",s_info->seg_status);
		printf("x = %d \n",s_info->x);
		printf("y = %d \n",s_info->y);
		printf("z = %d \n",s_info->z);
	
		printf("temperature = %f \n",s_info->temperature);
	
		printf("temperatureMIN = %f \n",s_info->temperatureMIN);
		printf("temperatureMAX = %f \n",s_info->temperatureMAX);
		printf("humidity = %f \n",s_info->humidity);
		printf("humidityMIN = %f \n",s_info->humidityMIN);
		printf("humidityMAX = %f \n",s_info->humidityMAX);	
	
		printf("illumination = %f \n",s_info->illumination);
		printf("illuminationMIN = %f \n",s_info->illuminationMIN);
		printf("illuminationMAX = %f \n",s_info->illuminationMAX);
	
		printf("battery = %f \n",s_info->battery);
		printf("adc = %f \n",s_info->adc);
#if 0	
		for(i=0;i <GOODS_NUM;i++){
			printf("goods_info[%d].type = %u\n",i,s_info->goods_info[i].goods_type);
			printf("goods_info[%d].count = %u\n",i,s_info->goods_info[i].goods_count);
		}	
#endif 

}


